import {
  getCurrentUserProfileForEdit,
  updateCurrentUserProfile,
  changeLanguage,
  changePassword,
  getProfilePicture,
  updateProfileRawPicture,
  getProfilePictureById
} from '@/api/profile'

const state = {
  avatar:
    '',
  uploadUrl: `${process.env.VUE_APP_BASE_API}/Profile/UploadProfilePicture`
}

const mutations = {
  SET_TOKEN: (state, token) => {
    // state.token = token
  }
}

const actions = {
  getCurrentUserProfileForEdit({ state }) {
    return new Promise((resolve, reject) => {
      getCurrentUserProfileForEdit()
        .then(response => {
          resolve(response.data.result)
        })
        .catch(error => {
          reject(error)
        })
    })
  },

  updateCurrentUserProfile({ state }, payload) {
    return new Promise((resolve, reject) => {
      updateCurrentUserProfile(payload.data)
        .then(response => {
          resolve(response.data.result)
        })
        .catch(error => {
          reject(error)
        })
    })
  },

  changeLanguage({ state }, payload) {
    return new Promise((resolve, reject) => {
      changeLanguage(payload.data)
        .then(() => {
          abp.utils.setCookieValue(
            'Abp.Localization.CultureName',
            payload.data.languageName,
            new Date(new Date().getTime() + 5 * 365 * 86400000),
            abp.appPath
          )
          window.location.reload()
        })
        .catch(error => {
          reject(error)
        })
    })
  },

  changePassword({ state }, payload) {
    return new Promise((resolve, reject) => {
      changePassword(payload.data)
        .then(response => {
          resolve(response.data.result)
        })
        .catch(error => {
          reject(error)
        })
    })
  },

  getProfilePicture({ state }) {
    return new Promise((resolve, reject) => {
      getProfilePicture()
        .then(response => {
          if (response.data.result.profilePicture.length) {
            state.avatar =
              'data:image/png;base64,' + response.data.result.profilePicture
          }
          resolve(response.data.result)
        })
        .catch(error => {
          reject(error)
        })
    })
  },

  getProfilePictureById({ state, rootState }, profilePictureId) {
    return new Promise((resolve, reject) => {
      getProfilePictureById(profilePictureId)
        .then(response => {
          if (response.data.result.profilePicture.length) {
            rootState.user.avatar =
              'data:image/png;base64,' + response.data.result.profilePicture
          }
          resolve(response.data.result)
        })
        .catch(error => {
          reject(error)
        })
    })
  },

  updateProfileRawPicture({ state, dispatch }, payload) {
    return new Promise((resolve, reject) => {
      updateProfileRawPicture(payload.data)
        .then(response => {
          dispatch('getProfilePicture')
          resolve(response.data.result)
        })
        .catch(error => {
          reject(error)
        })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
